package com.zhu.ttwords.view.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.zhu.ttwords.R;
import com.zhu.ttwords.TTWORDS;
import com.zhu.ttwords.presenter.SettingPresenter;

public class SettingActivity extends ActionBarActivity implements ISettingActivity, SeekBar.OnSeekBarChangeListener, View.OnClickListener {

    SeekBar group;
    ImageView groupRest;
    TextView groupCount;
    SettingPresenter presenter;
    int currentCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        group = (SeekBar) this.findViewById(R.id.activity_setting_group);
        groupRest = (ImageView) this.findViewById(R.id.activity_setting_grouop_reset);
        groupCount = (TextView) this.findViewById(R.id.activity_setting_group_count);
        currentCount = TTWORDS.getAppSetting().getGroup();
        groupCount.setText(currentCount + "");
        group.setProgress(currentCount / 5 - 1);
        group.setOnSeekBarChangeListener(this);
        groupRest.setOnClickListener(this);
        presenter = new SettingPresenter(this);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.finish();
            return false;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        currentCount = 5 * (progress + 1);
        groupCount.setText(currentCount + "");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        presenter.setGroup(currentCount);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_setting_grouop_reset:
                presenter.resetGroup();
                break;
        }
    }

    @Override
    public void resetGroup() {
        group.setProgress(TTWORDS.DEFAULT_STUDY_GROUP / 5 - 1);
    }
}
